namespace Billing
{
    using AjaxControlToolkit;
    using Microsoft.Reporting.WebForms;
    using Billing.Controls;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Microsoft.Security.Application;
    using System.Configuration;

    public class GenerateBillReport : Page
    {
        protected Button BtnGenerate;
        protected ConfirmButtonExtender BtnGenerate_ConfirmButtonExtender;
        protected CheckBox chkboxOverwriteReport;
        protected DropDownList ddlBillPeriod;
        protected DropDownList ddlCostCentre;
        protected DropDownList ddlAgency;
        protected DropDownList ddlBU;
        private DataTable dt;
        protected GridView gvwReports;
        protected ReportViewer ReportViewer1;
        protected ucHighlight ucHighlight1;
        protected ucPageTitle ucPageTitle1;
        protected UpdatePanel updatePanel1;
        protected UpdatePanel updatePanel2;
        protected UpdatePanel updatePanel7;
        protected UpdateProgress updateprogress1;

        private void BindBillPeriod()
        {
            ClsBillPeriod cls1 = new ClsBillPeriod();
            this.ddlBillPeriod.DataSource = cls1.SelectGeneratedDataPeriod();
            this.ddlBillPeriod.DataTextField = "ServiceMonth";
            this.ddlBillPeriod.DataValueField = "ID";
            this.ddlBillPeriod.DataBind();
            DataTable dtCurrentPeriod = cls1.SelectCurrentPreiod();
            if ((dtCurrentPeriod.Rows != null) && (dtCurrentPeriod.Rows.Count > 0))
            {
                this.BillPeriodID = dtCurrentPeriod.Rows[0]["ID"].ToString();
            }
        }

        private void BindDLLCostCentre()
        {
            ClsCostCentresInBU cls = new ClsCostCentresInBU();
            if (this.ddlBU.SelectedIndex <= 0)
            {
                cls.BUID = 0;
            }
            else
            {
                cls.BUID = Convert.ToInt64(this.ddlBU.SelectedValue);
            }
            DataTable dt1 = cls.SelectByBUID();
            this.ddlCostCentre.Items.Clear();
            this.ddlCostCentre.Items.Add(new ListItem("ALL", "0"));
            for (int i = 0; i < dt1.Rows.Count; i++)
            {
                this.ddlCostCentre.Items.Add(new ListItem(Encoder.HtmlEncode(dt1.Rows[i]["CostCentreIDName"].ToString()), Encoder.HtmlEncode(dt1.Rows[i]["ID"].ToString())));
            }
        }

        private void BindDLLAgency()
        {
            DataTable dt1 = new ClsAgency().SelectAll();
            this.ddlAgency.Items.Clear();
            this.ddlAgency.Items.Add(new ListItem("ALL", "0"));
            for (int i = 0; i < dt1.Rows.Count; i++)
            {
                this.ddlAgency.Items.Add(new ListItem(Encoder.HtmlEncode(dt1.Rows[i]["AgencyIDName"].ToString()), Encoder.HtmlEncode(dt1.Rows[i]["ID"].ToString())));
            }
            this.BindDLLBUsInAgency();
        }

        private void BindDLLBUsInAgency()
        {
            ClsBUsInAgency cls = new ClsBUsInAgency();
            if (this.ddlAgency.SelectedIndex <= 0)
            {
                cls.AgencyID = 0;
            }
            else
            {
                cls.AgencyID = Convert.ToInt64(this.ddlAgency.SelectedValue);
            }
            DataTable dt1 = cls.SelectByAgencyID();
            this.ddlBU.Items.Clear();
            this.ddlBU.Items.Add(new ListItem("ALL", "0"));
            for (int i = 0; i < dt1.Rows.Count; i++)
            {
                this.ddlBU.Items.Add(new ListItem(Encoder.HtmlEncode(dt1.Rows[i]["BUIDName"].ToString()), Encoder.HtmlEncode(dt1.Rows[i]["ID"].ToString())));
            }
            this.BindDLLCostCentre();
        }

        private DataTable BindGridViewData()
        {
            this.dt = new ClsBillReportTemplate().SelectAllActive();
            if (this.dt != null)
            {
                this.gvwReports.DataSource = this.dt;
                this.gvwReports.DataBind();
            }
            return this.dt;
        }

        protected void BtnGenerate_Click(object sender, EventArgs e)
        {
            try
            {
                this.ucHighlight1.CloseAll();
                int iCheckCount = 0;
                foreach (GridViewRow row in this.gvwReports.Rows)
                {
                    CheckBox chkbox = (CheckBox)row.FindControl("GRChkBox");
                    if (chkbox.Checked)
                    {
                        iCheckCount++;
                    }
                }
                if (iCheckCount == 0)
                {
                    this.ucHighlight1.ErrorMessage = "Please select at least one report for processing";
                }
                else if (this.ddlBillPeriod.SelectedIndex == -1)
                {
                    this.ucHighlight1.ErrorMessage = "Please select the Billing Period";
                }
                else if (this.ddlAgency.SelectedIndex == -1)
                {
                    this.ucHighlight1.ErrorMessage = "Please select the Agency";
                }
                else if (this.ddlBU.SelectedIndex == -1)
                {
                    this.ucHighlight1.ErrorMessage = "Please select the BU";
                }
                else if (this.ddlCostCentre.SelectedIndex == -1)
                {
                    this.ucHighlight1.ErrorMessage = "Please select the Cost Centre";
                }
                else
                {
                    DataTable dtBU;
                    DataTable dtCC;
                    ClsBUsInAgency clsBU = new ClsBUsInAgency();
                    ClsCostCentresInBU clsCC = new ClsCostCentresInBU();
                    if (Convert.ToInt64(this.ddlAgency.SelectedValue) == 0)
                    {
                        dtBU = clsBU.SelectAll();
                        dtCC = clsCC.SelectAll();
                    }
                    else if (Convert.ToInt64(this.ddlBU.SelectedValue) == 0)
                    {
                        clsBU.AgencyID = Convert.ToInt64(this.ddlAgency.SelectedValue);
                        dtBU = clsBU.SelectByAgencyID();
                        dtCC = clsCC.SelectAgencyBUCCByAgencyID(Convert.ToInt64(this.ddlAgency.SelectedValue));
                    }
                    else
                    {
                        clsBU.ID = Convert.ToInt64(this.ddlBU.SelectedValue);
                        dtBU = clsBU.Select();
                        if (Convert.ToInt64(this.ddlCostCentre.SelectedValue) == 0)
                        {
                            clsCC.BUID = Convert.ToInt64(this.ddlBU.SelectedValue);
                            dtCC = clsCC.SelectByBUID();
                        }
                        else
                        {
                            clsCC.ID = Convert.ToInt64(this.ddlCostCentre.SelectedValue);
                            dtCC = clsCC.SelectAgencyBUCCByID();
                        }
                    }
                    if ((dtCC == null) || (dtCC.Rows.Count == 0))
                    {
                        this.ucHighlight1.ErrorMessage = "Please select the Cost Centre";
                    }
                    else if ((dtBU == null) || (dtBU.Rows.Count == 0))
                    {
                        this.ucHighlight1.ErrorMessage = "Please select the BU";
                    }
                    else if (!this.chkboxOverwriteReport.Checked && this.CheckIfReportExist(dtBU, dtCC))
                    {
                        this.ucHighlight1.ErrorMessage = "Existing Report(s) found on the selected billing period. Unable to proceed.  ";
                    }
                    else
                    {
                        int iReportCount = 0;
                        ClsBillReportTemplate clsBillRptTemp = new ClsBillReportTemplate();
                        foreach (GridViewRow row in this.gvwReports.Rows)
                        {
                            CheckBox chk1 = (CheckBox)row.FindControl("GRChkBox");
                            if (chk1.Checked)
                            {
                                Label lblReportTempID = (Label)row.FindControl("lblReportTemplateID");
                                clsBillRptTemp.ID = Convert.ToInt64(lblReportTempID.Text);
                                DataTable dtBillRptTemp = clsBillRptTemp.Select();
                                if ((dtBillRptTemp != null) && (dtBillRptTemp.Rows.Count > 0))
                                {
                                    iReportCount += this.GenerateReport(dtBillRptTemp, dtBU, dtCC);
                                }
                            }
                        }
                        if (iReportCount > 0)
                        {
                            this.ucHighlight1.Message = this.ucHighlight1.Message = iReportCount.ToString() + " report(s) had been generated successfully.";
                        }
                        else
                        {
                            this.ucHighlight1.Message = this.ucHighlight1.Message = iReportCount.ToString() + " report(s) had been generated.";
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                log4net.Logger.Error("Exception:\r\n Type:" + ex.GetType() + ";\r\n Message:" + ex.Message + ";\r\n Trace:" + ex.StackTrace);
            }
        }

        private bool CheckIfReportExist(DataTable dtBU, DataTable dtCC)
        {
            ClsBillReportTemplate clsBillRptTemp = new ClsBillReportTemplate();
            foreach (GridViewRow row in this.gvwReports.Rows)
            {
                CheckBox chk1 = (CheckBox) row.FindControl("GRChkBox");
                if (chk1.Checked)
                {
                    Label lblReportTempID = (Label) row.FindControl("lblReportTemplateID");
                    clsBillRptTemp.ID = Convert.ToInt64(lblReportTempID.Text);
                    DataTable dtBillRptTemp = clsBillRptTemp.Select();
                    if ((dtBillRptTemp != null) && (dtBillRptTemp.Rows.Count > 0))
                    {
                        long CostCentreID;
                        long BUID;
                        ClsBillReportRepository clsRepository;
                        DataTable dtRepo;
                        int i;
                        long iBillReportTemplateID = Convert.ToInt64(dtBillRptTemp.Rows[0]["ID"].ToString());
                        string sParameterType = dtBillRptTemp.Rows[0]["ParameterType"].ToString();
                        string sBillType = dtBillRptTemp.Rows[0]["BillType"].ToString();
                        if (sParameterType.ToUpper() == "COSTCENTRE")
                        {
                            i = 0;
                            while (i < dtCC.Rows.Count)
                            {
                                CostCentreID = Convert.ToInt64(dtCC.Rows[i]["ID"].ToString());
                                BUID = Convert.ToInt64(dtCC.Rows[i]["BUID"].ToString());
                                clsRepository = new ClsBillReportRepository {
                                    BillPeriodID = Convert.ToInt64(this.BillPeriodID),
                                    BillReportTemplateID = iBillReportTemplateID,
                                    BUID = BUID,
                                    CostCentreID = CostCentreID
                                };
                                dtRepo = clsRepository.SelectByBillPeriodIDBillReportTemplateIDBUIDCostCentreID();
                                if ((dtRepo != null) && (dtRepo.Rows.Count > 0))
                                {
                                    return true;
                                }
                                i++;
                            }
                        }
                        else if (sParameterType.ToUpper() == "BU")
                        {
                            i = 0;
                            while (i < dtBU.Rows.Count)
                            {
                                CostCentreID = 0;
                                BUID = Convert.ToInt64(dtBU.Rows[i]["ID"].ToString());
                                clsRepository = new ClsBillReportRepository {
                                    BillPeriodID = Convert.ToInt64(this.BillPeriodID),
                                    BillReportTemplateID = iBillReportTemplateID,
                                    BUID = BUID,
                                    CostCentreID = CostCentreID
                                };
                                dtRepo = clsRepository.SelectByBillPeriodIDBillReportTemplateIDBUIDCostCentreID();
                                if ((dtRepo != null) && (dtRepo.Rows.Count > 0))
                                {
                                    return true;
                                }
                                i++;
                            }
                        }
                        else if (sParameterType.ToUpper() == "ALL")
                        {
                            CostCentreID = 0;
                            BUID = 0;
                            clsRepository = new ClsBillReportRepository {
                                BillPeriodID = Convert.ToInt64(this.BillPeriodID),
                                BillReportTemplateID = iBillReportTemplateID,
                                BUID = BUID,
                                CostCentreID = CostCentreID
                            };
                            dtRepo = clsRepository.SelectByBillPeriodIDBillReportTemplateIDBUIDCostCentreID();
                            if ((dtRepo != null) && (dtRepo.Rows.Count > 0))
                            {
                                return true;
                            }
                        }                        
                    }
                }
            }
            return false;
        }

        protected void ddlAgency_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.ddlAgency.SelectedIndex != -1)
            {
                this.BindDLLBUsInAgency();
            }
        }

        protected void ddlBU_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.ddlBU.SelectedIndex != -1)
            {
                this.BindDLLCostCentre();
            }
        }

        private void DeleteBillReportRepository(DataTable dtRepo)
        {
            ClsBillReportRepository clsRepository = new ClsBillReportRepository();
            if ((dtRepo != null) && (dtRepo.Rows.Count > 0))
            {
                for (int i = 0; i < dtRepo.Rows.Count; i++)
                {
                    clsRepository.ID = Convert.ToInt64(dtRepo.Rows[i]["ID"].ToString());
                    clsRepository.Delete();
                }
            }
        }

        private DataTable ExecuteReportStoreProc(string StoredProcName)
        {
            SqlCommand cm = new SqlCommand {
                CommandText = StoredProcName,
                CommandType = CommandType.StoredProcedure
            };
            cm.Parameters.Add(new SqlParameter("@BillPeriodID", Convert.ToInt64(this.BillPeriodID)));
            DBManager dm = new DBManager();
            return dm.executeCommandDataTable(cm);
        }

        private DataTable ExecuteReportStoreProcByBillCustomer(string StoredProcName, long BillCustomerID)
        {
            SqlCommand cm = new SqlCommand {
                CommandText = StoredProcName,
                CommandType = CommandType.StoredProcedure
            };
            cm.Parameters.Add(new SqlParameter("@BillPeriodID", Convert.ToInt64(this.BillPeriodID)));
            cm.Parameters.Add(new SqlParameter("@BillCustomerID", BillCustomerID));
            DBManager dm = new DBManager();
            return dm.executeCommandDataTable(cm);
        }

        private DataTable ExecuteReportStoreProcByCC(string StoredProcName, long CostCentreID)
        {
            SqlCommand cm = new SqlCommand {
                CommandText = StoredProcName,
                CommandType = CommandType.StoredProcedure
            };
            cm.Parameters.Add(new SqlParameter("@BillPeriodID", Convert.ToInt64(this.BillPeriodID)));
            cm.Parameters.Add(new SqlParameter("@CostCentreID", CostCentreID));
            DBManager dm = new DBManager();
            return dm.executeCommandDataTable(cm);
        }

        private DataTable ExecuteReportStoreProcByBU(string StoredProcName, long BUID)
        {
            SqlCommand cm = new SqlCommand {
                CommandText = StoredProcName,
                CommandType = CommandType.StoredProcedure
            };
            cm.Parameters.Add(new SqlParameter("@BillPeriodID", Convert.ToInt64(this.BillPeriodID)));
            cm.Parameters.Add(new SqlParameter("@BUID", BUID));
            DBManager dm = new DBManager();
            return dm.executeCommandDataTable(cm);
        }

        private int GenerateReport(DataTable dtBillRptTemp, DataTable dtBU, DataTable dtCC)
        {
            this.Culture = ConfigurationManager.AppSettings["Culture"].ToString();

            Warning[] warnings;
            string[] streamids;
            string mimeType;
            string encoding;
            string filenameExtension;
            DataTable dtRepo;
            int i;
            int iCount = 0;
            long iBillReportTemplateID = Convert.ToInt64(dtBillRptTemp.Rows[0]["ID"].ToString());
            string sReportName = dtBillRptTemp.Rows[0]["ReportName"].ToString();
            string sReportPath = dtBillRptTemp.Rows[0]["ReportPath"].ToString();
            string sRenderFormat = dtBillRptTemp.Rows[0]["RenderFormat"].ToString();
            string sReportFileExt = dtBillRptTemp.Rows[0]["ReportFileExt"].ToString();
            string sContentType = dtBillRptTemp.Rows[0]["ContentType"].ToString();
            string sDatasetName = dtBillRptTemp.Rows[0]["DatasetName"].ToString();
            string sStoredProcName = dtBillRptTemp.Rows[0]["StoredProcName"].ToString();
            string sParameterType = dtBillRptTemp.Rows[0]["ParameterType"].ToString();
            string sBillType = dtBillRptTemp.Rows[0]["BillType"].ToString();

            if (sParameterType.ToUpper() == "COSTCENTRE")
            {
                for (i = 0; i < dtCC.Rows.Count; i++)
                {
                    long CostCentreID = Convert.ToInt64(dtCC.Rows[i]["ID"].ToString());
                    long BUID = Convert.ToInt64(dtCC.Rows[i]["BUID"].ToString());
                    string CostCentreIDName = dtCC.Rows[i]["CostCentreIDName"].ToString();
                    string BUIDName = dtCC.Rows[i]["BUIDName"].ToString();
                    string sFileName = (CostCentreIDName + "_" + sReportName + sReportFileExt).Replace(" ", "_");
                    DataTable dtData = this.ExecuteReportStoreProcByCC(sStoredProcName, CostCentreID);
                    if ((dtData != null) && (dtData.Rows.Count > 0))
                    {
                        ReportDataSource rds = new ReportDataSource(sDatasetName, dtData);
                        this.ReportViewer1.LocalReport.ReportPath = sReportPath;
                        this.ReportViewer1.LocalReport.DataSources.Clear();
                        this.ReportViewer1.LocalReport.DataSources.Add(rds);
                        ReportParameter rptParamFileName = new ReportParameter("FileName", sFileName);
                        this.ReportViewer1.LocalReport.SetParameters(rptParamFileName);
                        byte[] bytes = this.ReportViewer1.LocalReport.Render(sRenderFormat, null, out mimeType, out encoding, out filenameExtension, out streamids, out warnings);
                        ClsBillReportRepository clsRepository = new ClsBillReportRepository
                        {
                            BillPeriodID = Convert.ToInt64(this.BillPeriodID),
                            BillReportTemplateID = iBillReportTemplateID,
                            ReportFileName = sFileName,
                            ReportData = bytes,
                            ContentType = sContentType,
                            IsReleased = false,
                            BUID = BUID,
                            CostCentreID = CostCentreID,
                            CreatedBy = LoginUser.UserID,
                            DownloadID = ClsUtilityHelper.GenerateId(),
                            BillCustomerID = 0
                        };
                        if (this.chkboxOverwriteReport.Checked)
                        {
                            dtRepo = clsRepository.SelectByBillPeriodIDBillReportTemplateIDBUIDCostCentreID();
                            this.DeleteBillReportRepository(dtRepo);
                        }
                        clsRepository.Save();
                        iCount++;
                        System.Threading.Thread.Sleep(1000);
                    }
                }
                return iCount;
            }

            if (sParameterType.ToUpper() == "BU")
            {
                for (i = 0; i < dtBU.Rows.Count; i++)
                {
                    long BUID = Convert.ToInt64(dtBU.Rows[i]["ID"].ToString());
                    string BUIDName = dtBU.Rows[i]["BUIDName"].ToString();
                    string sFileName = (BUIDName + "_" + sReportName + sReportFileExt).Replace(" ", "_");
                    DataTable dtData = this.ExecuteReportStoreProcByBU(sStoredProcName, BUID);
                    if ((dtData != null) && (dtData.Rows.Count > 0))
                    {
                        ReportDataSource rds = new ReportDataSource(sDatasetName, dtData);
                        this.ReportViewer1.LocalReport.ReportPath = sReportPath;
                        this.ReportViewer1.LocalReport.DataSources.Clear();
                        this.ReportViewer1.LocalReport.DataSources.Add(rds);
                        ReportParameter rptParamFileName = new ReportParameter("FileName", sFileName);
                        this.ReportViewer1.LocalReport.SetParameters(rptParamFileName);
                        byte[] bytes = this.ReportViewer1.LocalReport.Render(sRenderFormat, null, out mimeType, out encoding, out filenameExtension, out streamids, out warnings);
                        ClsBillReportRepository clsRepository = new ClsBillReportRepository
                        {
                            BillPeriodID = Convert.ToInt64(this.BillPeriodID),
                            BillReportTemplateID = iBillReportTemplateID,
                            ReportFileName = sFileName,
                            ReportData = bytes,
                            ContentType = sContentType,
                            IsReleased = false,
                            BUID = BUID,
                            CostCentreID = 0,
                            CreatedBy = LoginUser.UserID,
                            DownloadID = ClsUtilityHelper.GenerateId(),
                            BillCustomerID = 0
                        };
                        if (this.chkboxOverwriteReport.Checked)
                        {
                            dtRepo = clsRepository.SelectByBillPeriodIDBillReportTemplateIDBUIDCostCentreID();
                            this.DeleteBillReportRepository(dtRepo);
                        }
                        clsRepository.Save();
                        iCount++;
                        System.Threading.Thread.Sleep(1000);
                    }
                }
                return iCount;
            }

            //if (sParameterType.ToUpper() == "ALL")
            //{
            //    CostCentreID = 0;
            //    BUID = 0;
            //    CostCentreIDName = "";
            //    BUIDName = "";
            //    BillCustomerID = 0;
            //    sFileName = (sReportName + sReportFileExt).Replace(" ", "_");
            //    dtData = this.ExecuteReportStoreProc(sStoredProcName);
            //    if ((dtData != null) && (dtData.Rows.Count > 0))
            //    {
            //        rds = new ReportDataSource(sDatasetName, dtData);
            //        this.ReportViewer1.LocalReport.ReportPath = sReportPath;
            //        this.ReportViewer1.LocalReport.DataSources.Clear();
            //        this.ReportViewer1.LocalReport.DataSources.Add(rds);
            //        rptParamFileName = new ReportParameter("FileName", sFileName);
            //        this.ReportViewer1.LocalReport.SetParameters(rptParamFileName);
            //        bytes = this.ReportViewer1.LocalReport.Render(sRenderFormat, null, out mimeType, out encoding, out filenameExtension, out streamids, out warnings);
            //        clsRepository = new ClsBillReportRepository {
            //            BillPeriodID = Convert.ToInt64(this.BillPeriodID),
            //            BillReportTemplateID = iBillReportTemplateID,
            //            ReportFileName = sFileName,
            //            ReportData = bytes,
            //            ContentType = sContentType,
            //            IsReleased = false,
            //            BUID = BUID,
            //            CostCentreID = CostCentreID,
            //            CreatedBy = LoginUser.UserID,
            //            DownloadID = ClsUtilityHelper.GenerateId(),
            //            BillCustomerID = BillCustomerID
            //        };
            //        if (this.chkboxOverwriteReport.Checked)
            //        {
            //            dtRepo = clsRepository.SelectByBillPeriodIDBillReportTemplateIDBUIDCostCentreID();
            //            this.DeleteBillReportRepository(dtRepo);
            //        }
            //        clsRepository.Save();
            //        iCount++;
            //    }
            //    return iCount;
            //}            
            return iCount;
        }

        private void GetPermission()
        {
            ClsACLHelper cls = new ClsACLHelper();
            string pageName = this.Page.ToString().Substring(4, this.Page.ToString().Substring(4).Length - 5) + ".aspx";
            cls.SetPermission(LoginUser.TreeMenu, pageName);
            this.BtnGenerate.Visible = cls.Create0;
            if (!cls.Read0)
            {
                base.Response.Redirect("logout.aspx",true);
            }
        }

        protected void GRChkALL_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox chkALL = (CheckBox) this.gvwReports.HeaderRow.FindControl("GRChkALL");
            foreach (GridViewRow row in this.gvwReports.Rows)
            {
                CheckBox chk1 = (CheckBox) row.FindControl("GRChkBox");
                if (chkALL.Checked)
                {
                    chk1.Checked = true;
                }
                else
                {
                    chk1.Checked = false;
                }
            }
        }

        protected void GRChkBox_CheckedChanged(object sender, EventArgs e)
        {
            bool bAllChecked = true;
            CheckBox chk1 = (CheckBox) sender;
            CheckBox chkALL = (CheckBox) this.gvwReports.HeaderRow.FindControl("GRChkALL");
            if (!chk1.Checked)
            {
                chkALL.Checked = false;
            }
            else if (chk1.Checked)
            {
                foreach (GridViewRow row in this.gvwReports.Rows)
                {
                    CheckBox chk2 = (CheckBox) row.FindControl("GRChkBox");
                    if (!chk2.Checked)
                    {
                        bAllChecked = false;
                    }
                }
                if (bAllChecked)
                {
                    chkALL.Checked = true;
                }
            }
        }

        protected void gvwReports_Sorting(object sender, GridViewSortEventArgs e)
        {
            this.dt = new DataView(this.BindGridViewData()) { Sort = e.SortExpression + " " + this.ViewState["SortDirection"].ToString() }.ToTable();
            if (this.ViewState["SortDirection"].ToString() == "ASC")
            {
                this.ViewState["SortDirection"] = "DESC";
            }
            else
            {
                this.ViewState["SortDirection"] = "ASC";
            }
            this.gvwReports.DataSource = this.dt;
            this.gvwReports.DataBind();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.ucHighlight1.CloseAll();
            if (this.Session["BUID"] == null)
            {
                base.Response.Redirect("default.aspx",true);
            }
            if (!this.Page.IsPostBack)
            {
                this.ViewState["SortDirection"] = "ASC";
                this.BindBillPeriod();
                this.BindDLLAgency();
                this.BindGridViewData();
                this.GetPermission();
            }
        }

        public string BillPeriodID
        {
            get
            {
                if (this.ddlBillPeriod.SelectedIndex == -1)
                {
                    return "0";
                }
                return this.ddlBillPeriod.SelectedValue;
            }
            set
            {
                this.ddlBillPeriod.SelectedIndex = -1;
                if ((value != "") && (value != "0"))
                {
                    this.ddlBillPeriod.SelectedIndex = this.ddlBillPeriod.Items.IndexOf(this.ddlBillPeriod.Items.FindByValue(value));
                }
            }
        }
    }
}
